// pages/exchange/exchange.js

const host = require('../../config.js').host;
const network = require('../../utils/network.js');
 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    host:host,
    page:1,
    TabCur: 0,
    ownerId:'',
    exchangeList: [],
    exchangeHistoryList: [],
    TabItem: ["兑换大厅", "兑换记录"],
    isNoMoreData:false
  },
  tabSelect(e) {
    this.setData({
      page:1,
      TabCur: e.currentTarget.dataset.id,
      isNoMoreData:false
    })
    if(this.data.TabCur==0){
      this.getList();
    }else{
      this.getHistoryList();
    }
    
  },
  getList() {
    let that = this;
    // 参数： pageNum、pageSize、isExchange:1、goodsOwnerId（所属业主id）
 
    network.requestGet(host + '/xcx/goods/goods-list', {
      "goodsOwnerId": that.data.ownerId,
      "isExchange": "1",
      "pageNum":that.data.page,
      "pageSize":10
    }, res => { 
      if (res.data.code == 200) {
        if(that.data.page==1){
          that.setData({
            exchangeList: res.data.rows,
          })
          wx.stopPullDownRefresh()//得到结果后关掉刷新动画

        }else{
          that.setData({
            exchangeList: that.data.list.concat(res.data.rows),
          })
        }
        if(res.data.rows.length<10){
          that.setData({
            isNoMoreData:true
          })
        }else{
          that.setData({
            isNoMoreData:false
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }                                 
    })
  },
  getHistoryList() {
    let that = this;
    network.requestGet(host + '/xcx/customer-prize/aduit-list', {
      "from":'1',
      "prizeSource": '1',
      "pageNum":that.data.page,
      "pageSize":10
    }, res => { 
      if (res.data.code == 200) {
        if(that.data.page==1){
          that.setData({
            exchangeHistoryList: res.data.rows,
          })
          wx.stopPullDownRefresh()//得到结果后关掉刷新动画

        }else{
          that.setData({
            exchangeHistoryList: that.data.list.concat(res.data.rows),
          })
        }
        if(res.data.rows.length<10){
          that.setData({
            isNoMoreData:true
          })
        }else{
          that.setData({
            isNoMoreData:false
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }                           
    })
  },
  getInfo() {
    let that = this;
    network.requestGet(host + '/xcx/customer/getUserInfo', {}, res => { 
      if (res.data.code == 200) {
        that.setData({ 
          ownerId:res.data.data.ownerId
        }) 
        that.getList()
      } else { 
      }
    })
  },
  gotoExchangeDetail(e){
    
    wx.navigateTo({
      url: '/pages/exchangedetail/exchangedetail?goodsId='+e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:1
    })
    if(this.data.TabCur==0){
      this.getList();
    }else{
      this.getHistoryList();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page=this.data.page+1
    this.setData({
      page:page
    })
    if(this.data.TabCur==0){
      this.getList();
    }else{
      this.getHistoryList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})